package com.example.composestudy.part6

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.tooling.preview.Preview
import com.example.composestudy.utils.initialPage
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState

@Preview(showBackground = true)
@OptIn(ExperimentalPagerApi::class)
@Composable
fun DrawPath() {
    val pageCount = 2
    val initialPage = 0.initialPage()
    val state = rememberPagerState(initialPage)
    HorizontalPager(count = pageCount, modifier = Modifier.fillMaxWidth(), state = state) { page ->
        when (page) {
            0 -> DrawPath1()
            1 -> DrawPath2()
        }
    }
}

@Composable
fun DrawPath1() {
    PageItem(text = "DrawPath1") {
        val path = Path().apply {
            moveTo(100f, 300f)
            lineTo(100f, 700f)
            lineTo(800f, 700f)
            lineTo(900f, 300f)
            lineTo(600f, 100f)
            close()
        }
        Canvas(modifier = Modifier.fillMaxSize()) {
            drawPath(
                path = path,
                color = Color.Red,
                style = Stroke(width = 10f)
            )
        }
    }
}

@Composable
fun DrawPath2() {
    PageItem(text = "DrawPath2") {
        val path = Path().apply {
            moveTo(100f, 300f)
            lineTo(100f, 700f)
            quadraticBezierTo(800f, 600f, 600f, 100f)
            cubicTo(700f, 200f, 800f, 400f, 100f, 100f)
            close()
        }
        Canvas(modifier = Modifier.fillMaxSize()) {
            drawPath(
                path = path,
                color = Color.Red,
                style = Stroke(width = 10f)
            )
        }
    }
}
